package com.system.untils;

import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import java.util.concurrent.ThreadLocalRandom;

public class EmailVerification {
    public EmailVerification() {
    }

    public static String generateVerificationCode() {
        StringBuilder code = new StringBuilder();
        for (int i = 0; i < 4; i++) {
            code.append(ThreadLocalRandom.current().nextInt(0, 10));
        }
        return code.toString();
    }

    // 发送邮件
    public static void sendEmail(String toEmail, String subject, String body, Properties config) {
        // 获取SMTP配置
        String smtpHost = config.getProperty("smtp.host");
        String smtpPort = config.getProperty("smtp.port");
        String username = config.getProperty("smtp.username");
        String password = config.getProperty("smtp.password");

        // 设置邮件服务器的属性
        Properties properties = new Properties();
        properties.put("mail.smtp.auth", "true"); // 启用认证
        properties.put("mail.smtp.starttls.enable", "true"); // 启用TLS
        properties.put("mail.smtp.host", smtpHost); // SMTP服务器地址
        properties.put("mail.smtp.port", smtpPort); // SMTP端口

        // 获取默认的邮件会话对象
        Session session = Session.getInstance(properties, new Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(username, password); // 使用QQ邮箱授权码
            }
        });

        try {
            // 创建邮件内容
            MimeMessage message = new MimeMessage(session);
            message.setFrom(new InternetAddress(username)); // 发件人
            message.addRecipient(Message.RecipientType.TO, new InternetAddress(toEmail)); // 收件人
            message.setSubject(subject); // 邮件主题
            message.setText(body); // 邮件正文

            // 发送邮件
            Transport.send(message);
        } catch (MessagingException e) {
            throw new RuntimeException(e);
        }
    }

    // 读取配置文件
    public static Properties loadEmailConfig(String configFilePath) {
        Properties config = new Properties();
        try (InputStream inputStream = EmailVerification.class.getClassLoader().getResourceAsStream(configFilePath)) {
            if (inputStream == null) {
                throw new FileNotFoundException("Property file '" + configFilePath + "' not found in the classpath");
            }
            config.load(inputStream);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return config;
    }

    public String send(String email) {

        // 读取配置文件
        String configFilePath = "emailConfig.properties"; // 配置文件路径
        Properties config = loadEmailConfig(configFilePath);
        // 收件人邮箱地址
        String subject = "VerificationCode For Lake Of Letters";          // 邮件主题
        String verificationCode = generateVerificationCode(); // 生成验证码
        String body = "VerificationCode: " + verificationCode; // 邮件正文

        // 发送邮件
        sendEmail(email, subject, body, config);
        return verificationCode;
    }

}